package com.neusoft.elmboot.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.elmboot.mapper.WalletMapper;
import com.neusoft.elmboot.mapper.WalletEscMapper;
import com.neusoft.elmboot.po.Wallet;
import com.neusoft.elmboot.po.WalletEsc;
import com.neusoft.elmboot.service.WalletService;
import com.neusoft.elmboot.util.CommonUtil;

@Service
public class WalletServiceImpl implements WalletService{
	
	@Autowired
	private WalletMapper walletMapper;
	
    @Autowired
	private WalletEscMapper walletEscMapper;// 使用@Autowired自动注入
	
	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ)
	public Integer rechargeWallet(Wallet wallet) {
		walletMapper.rechargeWallet(wallet);
		return createWalletEsc(wallet, 1);
	}
	
	@Override
	public Integer payoutWallet(Wallet wallet) {
		walletMapper.payoutWallet(wallet);
		 return createWalletEsc(wallet, 2);
	}
	
	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ)
	public Integer payWallet(WalletEsc walletEsc) {
		return 0;
	}

     /**
     * 创建并保存交易记录
     * @param wallet 钱包实体
     * @param type 交易类型
     * @return 创建的交易记录ID
     */
    private Integer createWalletEsc(Wallet wallet, int type) {
        WalletEsc walletEsc = new WalletEsc();
        walletEsc.setEscTime(CommonUtil.getCurrentDate());
        walletEsc.gnrtEsc(wallet, type);
        walletEscMapper.saveWalletEsc(walletEsc); // 保存交易记录
        return walletEsc.getEscId();
    }
}